import React from 'react';
import { Modal, Button, Form, Select } from 'antd';

const Item = Form.Item;
const Option = Select.Option;

@Form.create()
class Components extends React.Component {
  /**
   * 提交表单到父组件
   */
  onOk = () => {
    this.props.form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        this.props.submit(values);
      }
    });
  };
  clear = () => {
    this.props.form.resetFields();
  };

  render() {
    const {
      visible,
      list,
      close,
      form: { getFieldDecorator },
    } = this.props;

    const formItemLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 8 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 12 },
      },
    };
    return (
      <Modal
        title="同步设备"
        afterClose={this.clear}
        visible={visible}
        onCancel={close}
        okText="确定"
        cancelText="取消"
        onOk={this.onOk}
      >
        <Form>
          <Item {...formItemLayout} label="同步至设备:">
            {getFieldDecorator('deviceIds', {
              rules: [
                {
                  required: true,
                  message: '请选择需要同步的设备',
                },
              ],
            })(
              <Select mode="multiple">
                {list.length > 0 && list.map(item => <Option key={item.id}>{item.deviceName}</Option>)}
              </Select>
            )}
          </Item>
        </Form>
      </Modal>
    );
  }
}

export default Components;
